package com.micai.springboot.controller;

import com.micai.springboot.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * 邮件 Controller 实现 Restful HTTP 服务
 *
 * @author 赵新国 on 2017/8/21.
 */
@RestController
public class MailRestController {

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    @RequestMapping(value = "/api/mail", method = RequestMethod.POST)
    public void sendSimpleMail(){
        // 1.发送简单邮件
        /*mailService.sendSimpleMail("zhaoxg@12366.com","test simple mail"," hello this is simple mail");*/

        // 2.发送html格式邮件
        /*String content="<html>\n" +
                "<body>\n" +
                "    <h3>hello world ! 这是一封Html邮件!</h3>\n" +
                "</body>\n" +
                "</html>";
        mailService.sendHtmlMail("zhaoxg@12366.com", "test simple mail", content);*/

        // 3.发送带附件的邮件
        /*String filePath="D:\\data\\logs\\boss_auth_api_consumer.log";
        mailService.sendAttachmentsMail("zhaoxg@12366.com", "主题：带附件的邮件", "有附件，请查收！", filePath);*/

        // 4.发送带静态资源的邮件
        /*String rscId = "neo006";
        String content="<html><body>这是有图片的邮件：<img src=\'cid:" + rscId + "\' ></body></html>";
        String imgPath = "D:\\系统架构图.png";
        mailService.sendInlineResourceMail("zhaoxg@12366.com", "主题：这是有图片的邮件", content, imgPath, rscId);*/

        // 解析模板并发送
        //创建邮件正文
        Context context = new Context();
        context.setVariable("id", "006");
        String emailContent = templateEngine.process("emailTemplate", context);

        mailService.sendHtmlMail("zhaoxg@12366.com","主题：这是模板邮件",emailContent);

    }

}
